<?php
session_cache_limiter('private, must-revalidate');
require '../check/check.php';
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>试题列表</title>
    <link rel="stylesheet" href="../plugins/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../css/global.css" media="all">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/table.css"/>
    <script type="text/javascript" src="../../js/jquery-3.2.0.min.js"></script>
    <script src="../plugins/jquery.form.min.js"></script>
</head>
<body>
<div class="admin-main">
    <blockquote class="layui-elem-quote">
        <a href="#" class="layui-btn layui-btn-small" id="add">
            <i class="layui-icon">&#xe608;</i> 添加试题
        </a>
        <a href="#" class="layui-btn layui-btn-small" id="export">
            <i class="fa fa-shopping-cart" aria-hidden="true"></i> 导出到Excel</a>
        <div class="layui-input-inline" style="float:right;">
            <a href="#" class="layui-btn layui-btn-small" id="search">
                <i class="layui-icon">&#xe615;</i> 搜索
            </a>
        </div>
    </blockquote>
    <fieldset class="layui-elem-field">
        <legend>试题列表</legend>
        <div class="layui-field-box">
            <table class="site-table table-hover">
                <thead>
                <tr>
                    <th style="text-align: center">试题标题
                    </th>
                    <th style="text-align: center">试题类型
                    </th>
                    <th style="text-align: center">试题科目</th>
                    <th style="text-align: center">试题归类</th>
                    <th style="text-align: center">难易度</th>
                    <th style="text-align: center">录入日期</th>
                    <th style="text-align: center">操作</th>
                </tr>
                </thead>
                <tbody id="content">
                </tbody>
            </table>
        </div>
    </fieldset>
    <div class="admin-table-page">
        <div id="paged" class="page">
        </div>
    </div>
</div>
<!--模板-->
<script type="text/html" id="tpl">
    {{# layui.each(d.list, function(index, item){ }}
    <tr>
        <td>{{ item.questiontitle }}</td>
        <td>{{ item.typename }}</td>
        <td>{{ item.questioncourseText }}</td>
        <td>{{ item.classifyname }}</td>
        <td>{{ item.difficultyText }}</td>
        <td>{{ item.inputdate }}</td>
        <td>
            <a href="javascript:;" data-name="{{ item.id }}" data-opt="edit"
               class="layui-btn layui-btn-mini">修改</a>
            <a href="javascript:;" data-name="{{ item.id }}" data-opt="del"
               class="layui-btn layui-btn-danger layui-btn-mini">删除</a>
            <a href="javascript:;" data-name="{{ item.id }}" data-opt="view"
               class="layui-btn layui-btn-mini">查看</a>
        </td>
    </tr>
    {{# }); }}
</script>
<script type="text/javascript" src="../plugins/layui/layui.js"></script>
<script>
    $(function () {
        getQuestionList('', '', '', '', '');
    });
    var editBoxIndex = -1;
    var addBoxIndex = -1;
    var viewBoxIndex = -1;
    var searchBoxIndex = -1;
    function editquestion() {
        var $that = $(this).parent().parent();
        id = $(this).data('name');
        classifyid = "";
        if (editBoxIndex != -1 || addBoxIndex != -1 || viewBoxIndex != -1 || searchBoxIndex != -1) {
            layer.msg('试题操作页面不能同时打开多个');
            return;
        }
        $.get('template/question.html', null, function (form) {
            editBoxIndex = layer.open({
                type: 1,
                title: '修改试题',
                content: form,
                btn: ['提交', '取消'],
                shade: false,
                offset: ['80px', '5%'],
                area: ['600px', '380px'],
                zIndex: 4,
                maxmin: true,
                yes: function (index) {
                    $('form.layui-form').find('button[lay-filter=save]').click();
                },
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                },
                success: function (layero, index) {
                    //弹出窗口成功后渲染表单
                    var form = layui.form();

                    $('form.layui-form').find('input[name=method]').val('updateQuestion');

                    $.ajax({
                        url: 'question.php',
                        type: "post",
                        dataType: "json",
                        data: 'method=getQuestionById&id=' + id,
                        success: function (result) {
                            $('form.layui-form').find('input[name=questiontitle]').val(result[0].questiontitle);
                            $('form.layui-form').find('input[name=questiontitle]').val(result[0].questiontitle);
                            $('form.layui-form').find('textarea[name=question]').val(result[0].question);
                            $('form.layui-form').find('textarea[name=analysis]').val(result[0].analysis);
                            classifyid = result[0].classifyid;
                            questioncourse = result[0].questioncourse;
                            difficulty = result[0].difficulty;
                            typeid = result[0].typeid;
                            $('form.layui-form').find('select[name=questiontype]').find('option[value=' + typeid + ']').attr('selected', 'selected');
                            form.render('select');

                            $('form.layui-form').find('select[name=questioncourse]').find('option[value=' + questioncourse + ']').attr('selected', 'selected');
                            form.render('select');

                            $('form.layui-form').find('select[name=difficulty]').find('option[value=' + difficulty + ']').attr('selected', 'selected');
                            form.render('select');

                            $.ajax({
                                url: 'question.php',
                                type: "post",
                                dataType: "json",
                                data: 'method=getClassify',
                                success: function (data) {
                                    $('#classify option:gt(0)').remove();
                                    var proHtml = '';
                                    for (var i = 0; i < data.length; i++) {
                                        proHtml += '<option value="' + data[i].classifyid + '" ';
                                        if (classifyid == data[i].classifyid) {
                                            proHtml += ' selected ';
                                        }
                                        proHtml += '>' + data[i].classifyname + '</option>';
                                    }
                                    $('#classify').append(proHtml);
                                    form.render();
                                }
                            });

                            if (result[0].imginfo != null && $.trim(result[0].imginfo) != '') {
                                $('#uploadimg').attr('src', '/OnLineExam/' + result[0].imginfo);
                            }

                            $('form.layui-form').find('input[name=id]').val(result[0].id);


                            if (typeid == 1) {
                                $('#ulchoice li:gt(1)').hide();
                                $('#ulchoice li:eq(1)').css('margin-bottom', "10px");
                                $('#rdoanswer li:gt(1)').hide();
                                $('#chkanswer li').hide();
                                $('#ulchoice li:lt(2)').find("input[name=choicea]").val(result[0].choicea);
                                $('#ulchoice li:lt(2)').find("input[name=choiceb]").val(result[0].choiceb);
                                $('#lblchoice').css('padding-top', '40px');


                                $('#rdoanswer').find('input[title=' + result[0].answer + ']').parent().find('div').addClass('layui-unselect layui-form-radio layui-form-radioed');
                                $('#rdoanswer').find('input[title=' + result[0].answer + ']').attr('checked', 'checked');
                                $('#rdoanswer').find('input[title=' + result[0].answer + ']').parent().find('i').addClass('layui-anim-scaleSpring');
                                $('#rdoanswer').find('input[title=' + result[0].answer + ']').parent().find('i').html('');

                            } else {
                                $('#lblchoice').css('padding-top', '85px');
                                //显示选项答案
                                $('#ulchoice').find('input[name=choicea]').val(result[0].choicea);
                                $('#ulchoice').find('input[name=choiceb]').val(result[0].choiceb);
                                $('#ulchoice').find('input[name=choicec]').val(result[0].choicec);
                                $('#ulchoice').find('input[name=choiced]').val(result[0].choiced);
                                if (typeid == 2) {
                                    $('#chkanswer li').hide();
                                    //设置单选按钮的状态
                                    $('#rdoanswer').find('input[title=' + result[0].answer + ']').parent().find('div').addClass('layui-unselect layui-form-radio layui-form-radioed');
                                    $('#rdoanswer').find('input[title=' + result[0].answer + ']').attr('checked', 'checked');
                                    $('#rdoanswer').find('input[title=' + result[0].answer + ']').parent().find('i').addClass('layui-anim-scaleSpring');
                                    $('#rdoanswer').find('input[title=' + result[0].answer + ']').parent().find('i').html('');
                                } else {
                                    $('#rdoanswer li').hide();
                                    //设置复选框选中
                                    answer = result[0].answer.split(',');
                                    $.each(answer, function (index, value) {
                                        $('#chkanswer').find('input[title=' + value + ']').parent().find('div').addClass('layui-unselect layui-form-checkbox layui-form-checked');
                                        $('#chkanswer').find('input[title=' + value + ']').attr('checked', 'checked');
                                    });
                                }
                            }
                            $('form.layui-form').find('input[name=questiontitle]').focus();
                        }
                    });


                    //绑定指定下拉列表框的change事件，通过下拉列表中指定的lay-filter属性值进行过滤绑定
                    form.on('select(questiontype)', function (data) {


                        $('#rdoanswer').find('input[type=radio]').parent().find('div').removeClass('layui-unselect layui-form-radioed');
                        $('#rdoanswer').find('input[type=radio]').removeAttr('checked');
                        $('#rdoanswer').find('input[type=radio]').parent().find('i').removeClass('layui-anim-scaleSpring');
                        $('#rdoanswer').find('input[type=radio]').parent().find('i').html('');


                        if (data.value == -1) {
                            $('div[id=choice]').hide();
                            $('div[id=rightanswer]').hide();
                            $('#chkanswer').find('input[type=checkbox]').parent().find('div').removeClass('layui-form-checked');
                        } else {
                            $('div[id=choice]').show();
                            $('div[id=rightanswer]').show();
                            $('#chkanswer li').show();
                            $('#rdoanswer li').show();
                            $('#ulchoice li').show();
                            $('#ulchoice li').find("input[name*=choice]").val('');
                            if (data.value == 1) {
                                $('#ulchoice li:gt(1)').hide();
                                $('#ulchoice li:eq(1)').css('margin-bottom', "10px");
                                $('#rdoanswer li:gt(1)').hide();
                                $('#chkanswer li').hide();
                                $('#lblchoice').css('padding-top', '40px');
                                $('#chkanswer').find('input[type=checkbox]').parent().find('div').removeClass('layui-form-checked');
                            } else {
                                $('#lblchoice').css('padding-top', '85px');
                                $('#ulchoice li:lt(2)').find("input[type=text]").val('');
                                if (data.value == 2) {
                                    $('#chkanswer li').hide();
                                    $('#chkanswer').find('input[type=checkbox]').parent().find('div').removeClass('layui-form-checked');
                                } else {
                                    $('#rdoanswer li').hide();
                                }
                            }
                        }
                    });

                    form.render();
                    form.on('submit(save)', function (data) {
                        $("form.layui-form").ajaxSubmit({
                            url: "question.php",
                            type: "post",
                            dataType: "json",
                            beforeSubmit: function (arr, $form, options) {
                            },
                            //提交成功后的回调函数
                            success: function (result, status, xhr, $form) {
                                layer.msg(result.msg, {time: 800}, function () {
                                    if (result.code == 0) {
                                        //修改表格中的数据
                                        $that.children().eq(0).html(result.editrec[0].questiontitle);
                                        $that.children().eq(1).html(result.editrec[0].typename);
                                        $that.children().eq(2).html(result.editrec[0].questioncourseText);
                                        $that.children().eq(3).html(result.editrec[0].classifyname);
                                        $that.children().eq(4).html(result.editrec[0].difficultyText);
                                        layer.closeAll();
                                        return false;
                                    } else {
                                        $('form.layui-form').find('input[name=questiontitle]').focus();
                                        return false;
                                    }
                                });
                            },
                            error: function (xhr, status, error, $form) {

                            },
                            complete: function (xhr, status, $form) {

                            }
                        });
                        return false;
                    });
                },
                end: function () {
                    editBoxIndex = -1;
                }
            });
            layer.full(editBoxIndex);
        });
    }
    function delquestion() {
        var $that = $(this).parent().parent();
        id = $(this).data('name');
        questiontitle = $that.children().eq(0).html();
        delmsg = '你确认要删除当前记录？<br>' + '试题标题：' + questiontitle;
        layer.confirm(delmsg, {icon: 3, title: '提示'}, function () {
            $.ajax({
                url: 'question.php',
                type: "post",
                dataType: "json",
                data: 'method=deleteQuestion&id=' + id,
                success: function (result) {
                    layer.msg(result.msg, {time: 800}, function () {
                        //删除表格中对应的数据行
                        if (result.errorCode == 0) {
                            $that.remove();
                            if ($('#content tr').length == 0 ){
                                location.reload();
                            }
                        }
                    });
                }
            });
        });
    }
    function viewquestion() {
        var $that = $(this).parent().parent();
        id = $(this).data('name');
        if (editBoxIndex != -1 || addBoxIndex != -1 || viewBoxIndex != -1 || searchBoxIndex != -1) {
            layer.msg('试题操作页面不能同时打开多个');
            return;
        }
        $.get('template/question_detail.html', null, function (form) {
            viewBoxIndex = layer.open({
                type: 1,
                title: '查看试题',
                content: form,
                btn: ['关闭'],
                shade: false,
                offset: ['80px', '5%'],
                area: ['600px', '380px'],
                zIndex: 4,
                maxmin: true,
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                },
                success: function (layero, index) {
                    //弹出窗口成功后渲染表单
                    var form = layui.form();
                    $.ajax({
                        url: 'question.php',
                        type: "post",
                        dataType: "json",
                        data: 'method=getQuestionById&id=' + id,
                        success: function (result) {
                            $('form.layui-form').find('input[name=questiontitle]').val(result[0].questiontitle);
                            $('form.layui-form').find('input[name=questioncourse]').val(result[0].questioncourseText);
                            $('form.layui-form').find('input[name=questiontype]').val(result[0].typename);
                            $('form.layui-form').find('input[name=classify]').val(result[0].classifyname);
                            $('form.layui-form').find('input[name=difficulty]').val(result[0].difficultyText);
                            $('#question').val(result[0].question);
                            $('form.layui-form').find('input[name=choicea]').val('答案选项A：' + result[0].choicea);
                            $('form.layui-form').find('input[name=choiceb]').val('答案选项B：' + result[0].choiceb);
                            if (result[0].typeid != 1) {
                                $('form.layui-form').find('input[name=choicec]').val('答案选项C：' + result[0].choicec);
                                $('form.layui-form').find('input[name=choiced]').val('答案选项D：' + result[0].choiced);
                            } else {
                                $('form.layui-form').find('input[name=choicec]').hide();
                                $('form.layui-form').find('input[name=choiced]').hide();
                            }
                            if (result[0].imginfo != null && $.trim(result[0].imginfo) != '') {
                                $('#uploadimg').attr('src', '/OnLineExam/' + result[0].imginfo);
                            } else {
                                $('#uploadpic').hide();
                            }
                            $('form.layui-form').find('input[name=answer]').val(result[0].answer);
                            $('#analysis').val(result[0].analysis);
                        }
                    });
                    form.render();
                },
                end: function () {
                    viewBoxIndex = -1;
                }
            });
            layer.full(viewBoxIndex);
        });
    }
    function getQuestionList(questiontitle, questioncourse, classify, questiontype, difficulty) {
        layui.config({
            base: '../js/'
        });
        layui.use(['paging', 'form'], function () {
            var $ = layui.jquery,
                paging = layui.paging(),
                layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
                layer = layui.layer, //获取当前窗口的layer对象
                form = layui.form();

            //页面表单元素验证
            form.verify({
                questioncourse: function (value) {
                    if (value == -1) {
                        return '必填项不能为空';
                    }
                },
                questiontype: function (value) {
                    if (value == -1) {
                        return '必填项不能为空';
                    }
                },
                classify: function (value) {
                    if (value == -1) {
                        return '必填项不能为空';
                    }
                },
                difficulty: function (value) {
                    if (value == -1) {
                        return '必填项不能为空';
                    }
                },
                choice: function (value) {
                    typeid = $('form.layui-form').find('select[name=questiontype]').val();
                    if (typeid != 1) {
                        if (value == '') {
                            return '必填项不能为空';
                        }
                    }
                },
                rdoanswer: function (value) {
                    typeid = $('form.layui-form').find('select[name=questiontype]').val();
                    ranswer = '';
                    if (typeid == 2) {
                        $('form.layui-form').find('input[type=radio]').each(function (k, v) {
                            if ($(v).parent().find('div').hasClass('layui-form-radioed')) {
                                ranswer = ranswer + v.value;
                            }
                        });
                        if (ranswer == '') {
                            return '试题答案不能为空';
                        }
                    } else if (typeid == 1) {
                        $('form.layui-form').find('input[type=radio]:lt(2)').each(function (k, v) {
                            if ($(v).parent().find('div').hasClass('layui-form-radioed')) {
                                ranswer = ranswer + v.value;
                            }
                        });
                        if (ranswer == '') {
                            return '试题答案不能为空';
                        }
                    }
                },
                chkanswer: function (value) {
                    typeid = $('form.layui-form').find('select[name=questiontype]').val();
                    if (typeid == 3) {
                        canswer = '';
                        $('form.layui-form').find('input[type=checkbox]').each(function (k, v) {
                            if ($(v).is(':checked')) {
                                canswer = canswer + v.value;
                            }
                        });
                        if (canswer == '') {
                            return '试题答案不能为空';
                        }
                    }
                }
            });


            paging.init({
                url: 'question.php', //地址
                elem: '#content', //内容容器
                params: { //发送到服务端的参数
                    method: 'getQuestionListByPage',
                    questiontitle: questiontitle,
                    questioncourse: questioncourse,
                    classify: classify,
                    questiontype: questiontype,
                    difficulty: difficulty
                },
                type: 'post',
                tempElem: '#tpl', //模块容器
                pageConfig: { //分页参数配置
                    elem: '#paged' //分页容器
//                  pageSize: 3 //使用paging.js配置的默认值15
                },
                success: function () { //渲染成功的回调
                },
                fail: function () { //获取数据失败的回调
                },
                complate: function () { //完成的回调
                    //绑定按钮事件
                    $('#content').children('tr').each(function (data) {
                        var $that = $(this);
                        $that.children('td:last-child').children('a[data-opt=edit]').on('click', editquestion);
                        $that.children('td:last-child').children('a[data-opt=del]').on('click', delquestion);
                        $that.children('td:last-child').children('a[data-opt=view]').on('click', viewquestion);
                    });
                },
            });
        });
    }
    $('#search').on('click', function () {
        if (editBoxIndex != -1 || addBoxIndex != -1 || viewBoxIndex != -1 || searchBoxIndex != -1) {
            layer.msg('试题操作页面不能同时打开多个');
            return;
        }
        $.get('template/question_search.html', null, function (form) {
            searchBoxIndex = layer.open({
                type: 1,
                title: '搜索条件',
                content: form,
                btn: ['搜索', '取消'],
                shade: false,
                offset: ['100px', '30%'],
                area: ['600px', '420px'],
                zIndex: 19950924,
                maxmin: true,
                yes: function (index) {
                    $('form.layui-form').find('button[lay-filter=search]').click();
                },
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                },
                success: function (layero, index) {
                    //弹出窗口成功后渲染表单
                    var form = layui.form();

                    //动态加载试题类型和试题归类下拉列表
                    $.ajax({
                        url: 'question.php',
                        type: "post",
                        dataType: "json",
                        data: 'method=getClassify',
                        success: function (data) {
                            $('#classify option:gt(0)').remove();
                            $.each(data, function (k, v) {
                                $('#classify').append('<option value=' + v.classifyid + '>' + v.classifyname + '</option>');
                            });
                            form.render('select');//渲染下拉列表框
                        }
                    });

                    form.render();
                    form.on('submit(search)', function (data) {
                        if (data.field.questioncourse == -1) {
                            data.field.questioncourse = "";
                        }
                        if (data.field.classify == -1) {
                            data.field.classify = "";
                        }
                        if (data.field.questiontype == -1) {
                            data.field.questiontype = "";
                        }
                        if (data.field.difficulty == -1) {
                            data.field.difficulty = "";
                        }
                        getQuestionList($.trim(data.field.questiontitle), $.trim(data.field.questioncourse), $.trim(data.field.classify), $.trim(data.field.questiontype), $.trim(data.field.difficulty));
                        layer.closeAll();
                        return false; //阻止表单跳转。
                    });
                },
                end: function () {
                    searchBoxIndex = -1;
                }
            });
        });
    });
    $('#add').on('click', function () {
        if (editBoxIndex != -1 || addBoxIndex != -1 || viewBoxIndex != -1 || searchBoxIndex != -1) {
            layer.msg('试题操作页面不能同时打开多个');
            return;
        }
        $.get('template/question.html', null, function (form) {
            addBoxIndex = layer.open({
                type: 1,
                title: '添加试题',
                content: form,
                btn: ['提交', '取消'],
                shade: false,
                offset: ['80px', '5%'],
                area: ['800px', '600px'],
                zIndex: 4,
                maxmin: true,
                yes: function (index) {
                    $('form.layui-form').find('button[lay-filter=save]').click();
                },
                full: function (elem) {
                    var win = window.top === window.self ? window : parent.window;
                    $(win).on('resize', function () {
                        var $this = $(this);
                        elem.width($this.width()).height($this.height()).css({
                            top: 0,
                            left: 0
                        });
                        elem.children('div.layui-layer-content').height($this.height() - 95);
                    });
                },
                success: function (layero, index) {
                    //弹出窗口成功后渲染表单
                    var form = layui.form();
                    $('#choice').hide();
                    $('#rightanswer').hide();

                    $('form.layui-form').find('input[name=method]').val('addQuestion');
                    //动态加载试题类型和试题归类下拉列表
                    $.ajax({
                        url: 'question.php',
                        type: "post",
                        dataType: "json",
                        data: 'method=getClassify',
                        success: function (data) {
                            $('#classify option:gt(0)').remove();
                            $.each(data, function (k, v) {
                                $('#classify').append('<option value=' + v.classifyid + '>' + v.classifyname + '</option>');
                            });
                            form.render('select');//渲染下拉列表框
                        }
                    });
                    //绑定指定下拉列表框的change事件，通过下拉列表中指定的lay-filter属性值进行过滤绑定
                    form.on('select(questiontype)', function (data) {
                        //重置单选按钮的状态
                        $('#rdoanswer').find('input[type=radio]').parent().find('div').removeClass('layui-unselect layui-form-radioed');
                        $('#rdoanswer').find('input[type=radio]').removeAttr('checked');
                        $('#rdoanswer').find('input[type=radio]').parent().find('i').removeClass('layui-anim-scaleSpring');
                        $('#rdoanswer').find('input[type=radio]').parent().find('i').html('');

                        if (data.value == -1) {
                            $('div[id=choice]').hide();
                            $('div[id=rightanswer]').hide();
                            $('#chkanswer').find('input[type=checkbox]').parent().find('div').removeClass('layui-form-checked');
                        } else {
                            $('div[id=choice]').show();
                            $('div[id=rightanswer]').show();
                            $('#chkanswer li').show();
                            $('#rdoanswer li').show();
                            $('#ulchoice li').show();
                            $('#ulchoice li').find("input[name*=choice]").val('');
                            if (data.value == 1) {
                                $('#ulchoice li:gt(1)').hide();
                                $('#ulchoice li:eq(1)').css('margin-bottom', "10px");
                                $('#rdoanswer li:gt(1)').hide();
                                $('#chkanswer li').hide();
                                $('#lblchoice').css('padding-top', '40px');
                                $('#chkanswer').find('input[type=checkbox]').parent().find('div').removeClass('layui-form-checked');
                            } else {
                                $('#lblchoice').css('padding-top', '85px');
                                $('#ulchoice li:lt(2)').find("input[type=text]").val('');
                                if (data.value == 2) {
                                    $('#chkanswer li').hide();
                                    $('#chkanswer').find('input[type=checkbox]').parent().find('div').removeClass('layui-form-checked');
                                } else {
                                    $('#rdoanswer li').hide();
                                }
                            }
                        }
                    });
                    form.render();
                    form.on('submit(save)', function (data) {
                        $("form.layui-form").ajaxSubmit({
                            url: "question.php",
                            type: "post",
                            dataType: "json",
                            beforeSubmit: function (arr, $form, options) {
                            },
                            //提交成功后的回调函数
                            success: function (result, status, xhr, $form) {
                                layer.msg(result.msg, {time: 800}, function () {
                                    if (result.code == 0) {
                                        $('#content').prepend('<tr></tr>');
                                        $('#content tr:first').append('<td>' + result.insertrec[0].questiontitle + '</td>');
                                        $('#content tr:first').append('<td>' + result.insertrec[0].typename + '</td>');
                                        $('#content tr:first').append('<td>' + result.insertrec[0].questioncourseText + '</td>');
                                        $('#content tr:first').append('<td>' + result.insertrec[0].classifyname + '</td>');
                                        $('#content tr:first').append('<td>' + result.insertrec[0].difficultyText + '</td>');
                                        $('#content tr:first').append('<td>' + result.insertrec[0].inputdate + '</td>');

                                        $('#content tr:first').append("<td><a href='javascript:;' data-opt='edit' " +
                                            "class='layui-btn layui-btn-mini' data-name='" + result.insertrec[0].id + "'>修改</a> " +
                                            "<a href='javascript:;' data-opt='del' " +
                                            "class='layui-btn layui-btn-danger layui-btn-mini' data-name='" + result.insertrec[0].id + "'>删除</a> " +
                                            "<a href='javascript:;' data-opt='view' " +
                                            "class='layui-btn layui-btn-mini' data-name='" + result.insertrec[0].id + "'>查看</a></td>");

                                        $('#content tr:first').children('td:last-child').children('a[data-opt=edit]').on('click', editquestion);
                                        $('#content tr:first').children('td:last-child').children('a[data-opt=del]').on('click', delquestion);
                                        $('#content tr:first').children('td:last-child').children('a[data-opt=view]').on('click', viewquestion);
                                        //重置页面数据
                                        $('form.layui-form').find('button[id=retbtn]').click();
                                        $('#uploadimg').removeAttr('src');
                                        $('#choice').hide();
                                        $('#rightanswer').hide();

                                        $('form.layui-form').find('input[name=questiontitle]').focus();
                                    } else {
                                        $('form.layui-form').find('input[name=questiontitle]').focus();
                                        return false;
                                    }
                                });
                            },
                            error: function (xhr, status, error, $form) {

                            },
                            complete: function (xhr, status, $form) {

                            }
                        });
                        return false;
                    });
                },
                end: function () {
                    addBoxIndex = -1;
                }
            });
            layer.full(addBoxIndex);
            //显示的时候直接最大化
        });
    });
    $('#export').on('click', function () {
        window.location.href = 'question.php?export=export';
    });
    //图片上传预览
    function preview(file) {
        if (file.files && file.files[0]) {
            if (!(/(?:jpg|gif|png|jpeg)$/i.test(file.value))) {
                alert("只允许上传jpg|gif|png|jpeg格式的图片");
                file.value = null;
                $('#uploadimg').removeAttr('src');
                return;
            }
            var reader = new FileReader();
            reader.onload = function (evt) {
                $('#uploadimg').attr('src', evt.target.result);
            }
            reader.readAsDataURL(file.files[0]);
        }
    }
</script>
</body>

</html>